//
//  HistoryOrderController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/24.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import DZNEmptyDataSet
import QMUIKit

class HistoryOrderController: UIViewController ,UITableViewDelegate,UITableViewDataSource,QMUINavigationControllerDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    
    let kHistoryOrderCellID = "kHistoryOrderCellID";
    
    var dataArray = [OrderListModel]();
    
    lazy var tableView : UITableView = {
        let table = UITableView.init(frame: self.view.bounds);
        
        table.backgroundColor = ZMColor.Gray.gray1;
        table.delegate = self;
        table.dataSource = self;
        table.register(UITableViewCell.self, forCellReuseIdentifier: kLeftMenuCellID);
        table.registerNib("OrderRobCell", kHistoryOrderCellID);
        table.showsVerticalScrollIndicator = false;
        table.separatorStyle = .none;
        table.estimatedRowHeight = 0;
        table.estimatedSectionFooterHeight = 0;
        table.estimatedSectionHeaderHeight = 0;
        table.emptyDataSetSource = self;
        table.emptyDataSetDelegate = self;
        if #available(iOS 11.0, *) {
            table.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
            self.automaticallyAdjustsScrollViewInsets = false;
        };
        return table;
    }();
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
        self.listDataRequest();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    // MARK: 👉🏻 🐷System Delegate🐷
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: kHistoryOrderCellID) as! OrderRobCell;
        let model = self.dataArray[indexPath.row];
        cell.typeLabel.text = model.typeDesc();
        cell.moneyLabel.text = model.orderPrice;
//        cell.orderBtn.isHidden = true;
        cell.orderBtn.isUserInteractionEnabled = false;
        cell.orderBtn.setTitle("已完成", for: .normal);
        return cell;
    }
    // MARK: 👉🏻 🐷Custom Delegate🐷
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let param = [
            NSAttributedString.Key.font : UIFont.init(fontType: .regular, fontSize: 15),
            
        ]
        return NSAttributedString.init(string: "暂无订单",attributes: param);
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return ZMImage("orderlist_empty");
    }
    
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true;
    }
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return -100;
    }
    // MARK: 👉🏻 🐷Event  Response🐷
    // MARK: 👉🏻 🐷Private Methods🐷
    //订单列表接口
    func  listDataRequest() {
        
        let param = ["orderStatus" : "3"];
        
        MoyaRequest(.orderList(param), success: { (response :BaseResponse<[OrderListModel]>) in
            if response.respCode == 200{
                self.dataArray = response.data!;
                self.tableView.reloadData();
            }else{
                
            }
            
        }) { (error) in
            
        }
    }
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        self.view.backgroundColor = ZMColor.Gray.gray1;
        self.navigationItem.title = "历史抢单";
    }
    func initSubViews()  {
        self.view.addSubview(self.tableView);
    }
    func layout()  {
        //列表
        self.tableView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview();
            make.leading.equalToSuperview().offset(10);
            make.trailing.equalToSuperview().offset(-10);
        };
    }
}
